home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / binutils.252 / gas / config / obj-aout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-14  |  14.0 KB  |  538 lines

  1. /* a.out object file format
  2.    Copyright (C) 1989, 1990, 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GAS, the GNU Assembler.
  5.  
  6. GAS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as
  8. published by the Free Software Foundation; either version 2,
  9. or (at your option) any later version.
  10.  
  11. GAS is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
  14. the GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public
  17. License along with GAS; see the file COPYING.  If not, write
  18. to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  19.  
  20. #include "as.h"
  21. #ifdef BFD_ASSEMBLER
  22. #undef NO_RELOC
  23. #include "aout/aout64.h"
  24. #endif
  25. #include "obstack.h"
  26.  
  27. #ifndef BFD_ASSEMBLER
  28. /* in: segT   out: N_TYPE bits */
  29. const short seg_N_TYPE[] =
  30. {
  31.   N_ABS,
  32.   N_TEXT,
  33.   N_DATA,
  34.   N_BSS,
  35.   N_UNDF,            /* unknown */
  36.   N_UNDF,            /* error */
  37.   N_UNDF,            /* expression */
  38.   N_UNDF,            /* debug */
  39.   N_UNDF,            /* ntv */
  40.   N_UNDF,            /* ptv */
  41.   N_REGISTER,            /* register */
  42. };
  43.  
  44. const segT N_TYPE_seg[N_TYPE + 2] =
  45. {                /* N_TYPE == 0x1E = 32-2 */
  46.   SEG_UNKNOWN,            /* N_UNDF == 0 */
  47.   SEG_GOOF,
  48.   SEG_ABSOLUTE,            /* N_ABS == 2 */
  49.   SEG_GOOF,
  50.   SEG_TEXT,            /* N_TEXT == 4 */
  51.   SEG_GOOF,
  52.   SEG_DATA,            /* N_DATA == 6 */
  53.   SEG_GOOF,
  54.   SEG_BSS,            /* N_BSS == 8 */
  55.   SEG_GOOF,
  56.   SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF,
  57.   SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF,
  58.   SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF,
  59.   SEG_REGISTER,            /* dummy N_REGISTER for regs = 30 */
  60.   SEG_GOOF,
  61. };
  62. #endif
  63.  
  64. static void obj_aout_line PARAMS ((int));
  65. static void obj_aout_weak PARAMS ((int));
  66.  
  67. const pseudo_typeS obj_pseudo_table[] =
  68. {
  69.   {"line", obj_aout_line, 0},    /* source code line number */
  70.   {"ln", obj_aout_line, 0},    /* coff line number that we use anyway */
  71.  
  72.   {"weak", obj_aout_weak, 0},    /* mark symbol as weak.  */
  73.  
  74.   /* coff debug pseudos (ignored) */
  75.   {"def", s_ignore, 0},
  76.   {"dim", s_ignore, 0},
  77.   {"endef", s_ignore, 0},
  78.   {"ident", s_ignore, 0},
  79.   {"line", s_ignore, 0},
  80.   {"ln", s_ignore, 0},
  81.   {"scl", s_ignore, 0},
  82.   {"size", s_ignore, 0},
  83.   {"tag", s_ignore, 0},
  84.   {"type", s_ignore, 0},
  85.   {"val", s_ignore, 0},
  86.   {"version", s_ignore, 0},
  87.  
  88.   {"optim", s_ignore, 0},    /* For sun386i cc (?) */
  89.  
  90.   /* other stuff */
  91.   {"ABORT", s_abort, 0},
  92.  
  93.   {NULL}            /* end sentinel */
  94. };                /* obj_pseudo_table */
  95.  
  96.  
  97. #ifdef BFD_ASSEMBLER
  98.  
  99. void
  100. obj_aout_frob_symbol (sym, punt)
  101.      symbolS *sym;
  102.      int *punt;
  103. {
  104.   flagword flags;
  105.   asection *sec;
  106.   int desc, type, other;
  107.  
  108.   flags = sym->bsym->flags;
  109.   desc = S_GET_DESC (sym);
  110.   type = S_GET_TYPE (sym);
  111.   other = S_GET_OTHER (sym);
  112.   sec = sym->bsym->section;
  113.  
  114.   /* Only frob simple symbols this way right now.  */
  115.   if (! (type & ~ (N_TYPE | N_EXT)))
  116.     {
  117.       if (type == (N_UNDF | N_EXT)
  118.       && sec == &bfd_abs_section)
  119.     sym->bsym->section = sec = bfd_und_section_ptr;
  120.  
  121.       if ((type & N_TYPE) != N_INDR
  122.       && (type & N_TYPE) != N_SETA
  123.       && (type & N_TYPE) != N_SETT
  124.       && (type & N_TYPE) != N_SETD
  125.       && (type & N_TYPE) != N_SETB
  126.       && type != N_WARNING
  127.       && (sec == &bfd_abs_section
  128.           || sec == &bfd_und_section))
  129.     return;
  130.       if (flags & BSF_EXPORT)
  131.     type |= N_EXT;
  132.  
  133.       switch (type & N_TYPE)
  134.     {
  135.     case N_SETA:
  136.     case N_SETT:
  137.     case N_SETD:
  138.     case N_SETB:
  139.       /* Set the debugging flag for constructor symbols so that
  140.          BFD leaves them alone.  */
  141.       sym->bsym->flags |= BSF_DEBUGGING;
  142.  
  143.       /* You can't put a common symbol in a set.  The way a set
  144.          element works is that the symbol has a definition and a
  145.          name, and the linker adds the definition to the set of
  146.          that name.  That does not work for a common symbol,
  147.          because the linker can't tell which common symbol the
  148.          user means.  FIXME: Using as_bad here may be
  149.          inappropriate, since the user may want to force a
  150.          particular type without regard to the semantics of sets;
  151.          on the other hand, we certainly don't want anybody to be
  152.          mislead into thinking that their code will work.  */
  153.       if (S_IS_COMMON (sym))
  154.         as_bad ("Attempt to put a common symbol into set %s",
  155.             S_GET_NAME (sym));
  156.  
  157.       break;
  158.     case N_INDR:
  159.       /* Put indirect symbols in the indirect section.  */
  160.       sym->bsym->section = bfd_ind_section_ptr;
  161.       sym->bsym->flags |= BSF_INDIRECT;
  162.       if (type & N_EXT)
  163.         {
  164.           sym->bsym->flags |= BSF_EXPORT;
  165.           sym->bsym->flags &=~ BSF_LOCAL;
  166.         }
  167.       break;
  168.     case N_WARNING:
  169.       /* Mark warning symbols.  */
  170.       sym->bsym->flags |= BSF_WARNING;
  171.       break;
  172.     }
  173.     }
  174.   else
  175.     {
  176.       sym->bsym->flags |= BSF_DEBUGGING;
  177.     }
  178.  
  179.   S_SET_TYPE (sym, type);
  180. }
  181.  
  182. void
  183. obj_aout_frob_file ()
  184. {
  185.   /* Relocation processing may require knowing the VMAs of the sections.
  186.      Since writing to a section will cause the BFD back end to compute the
  187.      VMAs, fake it out here....  */
  188.   bfd_byte b = 0;
  189.   boolean x = true;
  190.   if (bfd_section_size (stdoutput, text_section) != 0)
  191.     {
  192.       x = bfd_set_section_contents (stdoutput, text_section, &b, (file_ptr) 0,
  193.                     (bfd_size_type) 1);
  194.     }
  195.   else if (bfd_section_size (stdoutput, data_section) != 0)
  196.     {
  197.       x = bfd_set_section_contents (stdoutput, data_section, &b, (file_ptr) 0,
  198.                     (bfd_size_type) 1);
  199.     }
  200.   assert (x == true);
  201. }
  202.  
  203. #else
  204.  
  205. /* Relocation. */
  206.  
  207. /*
  208.  *        emit_relocations()
  209.  *
  210.  * Crawl along a fixS chain. Emit the segment's relocations.
  211.  */
  212. void
  213. obj_emit_relocations (where, fixP, segment_address_in_file)
  214.      char **where;
  215.      fixS *fixP;        /* Fixup chain for this segment. */
  216.      relax_addressT segment_address_in_file;
  217. {
  218.   for (; fixP; fixP = fixP->fx_next)
  219.     if (fixP->fx_done == 0)
  220.       {
  221.     tc_aout_fix_to_chars (*where, fixP, segment_address_in_file);
  222.     *where += md_reloc_size;
  223.       }
  224. }
  225.  
  226. #ifndef obj_header_append
  227. /* Aout file generation & utilities */
  228. void
  229. obj_header_append (where, headers)
  230.      char **where;
  231.      object_headers *headers;
  232. {
  233.   tc_headers_hook (headers);
  234.  
  235. #ifdef CROSS_COMPILE
  236.   md_number_to_chars (*where, headers->header.a_info, sizeof (headers->header.a_info));
  237.   *where += sizeof (headers->header.a_info);
  238.   md_number_to_chars (*where, headers->header.a_text, sizeof (headers->header.a_text));
  239.   *where += sizeof (headers->header.a_text);
  240.   md_number_to_chars (*where, headers->header.a_data, sizeof (headers->header.a_data));
  241.   *where += sizeof (headers->header.a_data);
  242.   md_number_to_chars (*where, headers->header.a_bss, sizeof (headers->header.a_bss));
  243.   *where += sizeof (headers->header.a_bss);
  244.   md_number_to_chars (*where, headers->header.a_syms, sizeof (headers->header.a_syms));
  245.   *where += sizeof (headers->header.a_syms);
  246.   md_number_to_chars (*where, headers->header.a_entry, sizeof (headers->header.a_entry));
  247.   *where += sizeof (headers->header.a_entry);
  248.   md_number_to_chars (*where, headers->header.a_trsize, sizeof (headers->header.a_trsize));
  249.   *where += sizeof (headers->header.a_trsize);
  250.   md_number_to_chars (*where, headers->header.a_drsize, sizeof (headers->header.a_drsize));
  251.   *where += sizeof (headers->header.a_drsize);
  252.  
  253. #else /* CROSS_COMPILE */
  254.  
  255.   append (where, (char *) &headers->header, sizeof (headers->header));
  256. #endif /* CROSS_COMPILE */
  257.  
  258. }
  259. #endif
  260.  
  261. void
  262. obj_symbol_to_chars (where, symbolP)
  263.      char **where;
  264.      symbolS *symbolP;
  265. {
  266.   md_number_to_chars ((char *) &(S_GET_OFFSET (symbolP)), S_GET_OFFSET (symbolP), sizeof (S_GET_OFFSET (symbolP)));
  267.   md_number_to_chars ((char *) &(S_GET_DESC (symbolP)), S_GET_DESC (symbolP), sizeof (S_GET_DESC (symbolP)));
  268.   md_number_to_chars ((char *) &(symbolP->sy_symbol.n_value), S_GET_VALUE (symbolP), sizeof (symbolP->sy_symbol.n_value));
  269.  
  270.   append (where, (char *) &symbolP->sy_symbol, sizeof (obj_symbol_type));
  271. }
  272.  
  273. void
  274. obj_emit_symbols (where, symbol_rootP)
  275.      char **where;
  276.      symbolS *symbol_rootP;
  277. {
  278.   symbolS *symbolP;
  279.  
  280.   /* Emit all symbols left in the symbol chain.  */
  281.   for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
  282.     {
  283.       /* Used to save the offset of the name. It is used to point
  284.      to the string in memory but must be a file offset. */
  285.       register char *temp;
  286.  
  287.       temp = S_GET_NAME (symbolP);
  288.       S_SET_OFFSET (symbolP, symbolP->sy_name_offset);
  289.  
  290.       /* Any symbol still undefined and is not a dbg symbol is made N_EXT. */
  291.       if (!S_IS_DEBUG (symbolP) && !S_IS_DEFINED (symbolP))
  292.     S_SET_EXTERNAL (symbolP);
  293.  
  294.       /* Adjust the type of a weak symbol.  */
  295.       if (S_GET_WEAK (symbolP))
  296.     {
  297.       switch (S_GET_TYPE (symbolP))
  298.         {
  299.         case N_UNDF: S_SET_TYPE (symbolP, N_WEAKU); break;
  300.         case N_ABS:     S_SET_TYPE (symbolP, N_WEAKA); break;
  301.         case N_TEXT: S_SET_TYPE (symbolP, N_WEAKT); break;
  302.         case N_DATA: S_SET_TYPE (symbolP, N_WEAKD); break;
  303.         case N_BSS:  S_SET_TYPE (symbolP, N_WEAKB); break;
  304.         default: as_bad ("%s: bad type for weak symbol", temp); break;
  305.         }
  306.     }
  307.  
  308.       obj_symbol_to_chars (where, symbolP);
  309.       S_SET_NAME (symbolP, temp);
  310.     }
  311. }
  312.  
  313. #endif /* ! BFD_ASSEMBLER */
  314.  
  315. static void
  316. obj_aout_line (ignore)
  317.      int ignore;
  318. {
  319.   /* Assume delimiter is part of expression.
  320.      BSD4.2 as fails with delightful bug, so we
  321.      are not being incompatible here. */
  322.   new_logical_line ((char *) NULL, (int) (get_absolute_expression ()));
  323.   demand_empty_rest_of_line ();
  324. }                /* obj_aout_line() */
  325.  
  326. /* Handle .weak.  This is a GNU extension.  */
  327.  
  328. static void
  329. obj_aout_weak (ignore)
  330.      int ignore;
  331. {
  332.   char *name;
  333.   int c;
  334.   symbolS *symbolP;
  335.  
  336.   do
  337.     {
  338.       name = input_line_pointer;
  339.       c = get_symbol_end ();
  340.       symbolP = symbol_find_or_make (name);
  341.       *input_line_pointer = c;
  342.       SKIP_WHITESPACE ();
  343.       S_SET_WEAK (symbolP);
  344.       if (c == ',')
  345.     {
  346.       input_line_pointer++;
  347.       SKIP_WHITESPACE ();
  348.       if (*input_line_pointer == '\n')
  349.         c = '\n';
  350.     }
  351.     }
  352.   while (c == ',');
  353.   demand_empty_rest_of_line ();
  354. }
  355.  
  356. void
  357. obj_read_begin_hook ()
  358. {
  359. }
  360.  
  361. #ifndef BFD_ASSEMBLER
  362.  
  363. void
  364. obj_crawl_symbol_chain (headers)
  365.      object_headers *headers;
  366. {
  367.   symbolS *symbolP;
  368.   symbolS **symbolPP;
  369.   int symbol_number = 0;
  370.  
  371.   tc_crawl_symbol_chain (headers);
  372.  
  373.   symbolPP = &symbol_rootP;    /*->last symbol chain link. */
  374.   while ((symbolP = *symbolPP) != NULL)
  375.     {
  376.       if (flag_readonly_data_in_text && (S_GET_SEGMENT (symbolP) == SEG_DATA))
  377.     {
  378.       S_SET_SEGMENT (symbolP, SEG_TEXT);
  379.     }            /* if pusing data into text */
  380.  
  381.       resolve_symbol_value (symbolP);
  382.  
  383.       /* OK, here is how we decide which symbols go out into the brave
  384.      new symtab.  Symbols that do are:
  385.  
  386.      * symbols with no name (stabd's?)
  387.      * symbols with debug info in their N_TYPE
  388.  
  389.      Symbols that don't are:
  390.      * symbols that are registers
  391.      * symbols with \1 as their 3rd character (numeric labels)
  392.      * "local labels" as defined by S_LOCAL_NAME(name) if the -L
  393.      switch was passed to gas.
  394.  
  395.      All other symbols are output.  We complain if a deleted
  396.      symbol was marked external. */
  397.  
  398.  
  399.       if (!S_IS_REGISTER (symbolP)
  400.       && (!S_GET_NAME (symbolP)
  401.           || S_IS_DEBUG (symbolP)
  402.           || !S_IS_DEFINED (symbolP)
  403.           || S_IS_EXTERNAL (symbolP)
  404.           || (S_GET_NAME (symbolP)[0] != '\001'
  405.           && (flag_keep_locals || !S_LOCAL_NAME (symbolP)))))
  406.     {
  407.       symbolP->sy_number = symbol_number++;
  408.  
  409.       /* The + 1 after strlen account for the \0 at the
  410.                end of each string */
  411.       if (!S_IS_STABD (symbolP))
  412.         {
  413.           /* Ordinary case. */
  414.           symbolP->sy_name_offset = string_byte_count;
  415.           string_byte_count += strlen (S_GET_NAME (symbolP)) + 1;
  416.         }
  417.       else            /* .Stabd case. */
  418.         symbolP->sy_name_offset = 0;
  419.       symbolPP = &(symbol_next (symbolP));
  420.     }
  421.       else
  422.     {
  423.       if (S_IS_EXTERNAL (symbolP) || !S_IS_DEFINED (symbolP))
  424.         /* This warning should never get triggered any more.
  425.            Well, maybe if you're doing twisted things with
  426.            register names...  */
  427.         {
  428.           as_bad ("Local symbol %s never defined.", decode_local_label_name (S_GET_NAME (symbolP)));
  429.         }            /* oops. */
  430.  
  431.       /* Unhook it from the chain */
  432.       *symbolPP = symbol_next (symbolP);
  433.     }            /* if this symbol should be in the output */
  434.     }                /* for each symbol */
  435.  
  436.   H_SET_SYMBOL_TABLE_SIZE (headers, symbol_number);
  437. }
  438.  
  439. /*
  440.  * Find strings by crawling along symbol table chain.
  441.  */
  442.  
  443. void
  444. obj_emit_strings (where)
  445.      char **where;
  446. {
  447.   symbolS *symbolP;
  448.  
  449. #ifdef CROSS_COMPILE
  450.   /* Gotta do md_ byte-ordering stuff for string_byte_count first - KWK */
  451.   md_number_to_chars (*where, string_byte_count, sizeof (string_byte_count));
  452.   *where += sizeof (string_byte_count);
  453. #else /* CROSS_COMPILE */
  454.   append (where, (char *) &string_byte_count, (unsigned long) sizeof (string_byte_count));
  455. #endif /* CROSS_COMPILE */
  456.  
  457.   for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
  458.     {
  459.       if (S_GET_NAME (symbolP))
  460.     append (&next_object_file_charP, S_GET_NAME (symbolP),
  461.         (unsigned long) (strlen (S_GET_NAME (symbolP)) + 1));
  462.     }                /* walk symbol chain */
  463. }
  464.  
  465. #ifndef AOUT_VERSION
  466. #define AOUT_VERSION 0
  467. #endif
  468.  
  469. void
  470. obj_pre_write_hook (headers)
  471.      object_headers *headers;
  472. {
  473.   H_SET_DYNAMIC (headers, 0);
  474.   H_SET_VERSION (headers, AOUT_VERSION);
  475.   H_SET_MACHTYPE (headers, AOUT_MACHTYPE);
  476.   tc_aout_pre_write_hook (headers);
  477. }
  478.  
  479. void
  480. DEFUN_VOID (s_sect)
  481. {
  482.   /* Strip out the section name */
  483.   char *section_name;
  484.   char *section_name_end;
  485.   char c;
  486.  
  487.   unsigned int len;
  488.   unsigned int exp;
  489.   char *save;
  490.  
  491.   section_name = input_line_pointer;
  492.   c = get_symbol_end ();
  493.   section_name_end = input_line_pointer;
  494.  
  495.   len = section_name_end - section_name;
  496.   input_line_pointer++;
  497.   save = input_line_pointer;
  498.  
  499.   SKIP_WHITESPACE ();
  500.   if (c == ',')
  501.     {
  502.       exp = get_absolute_expression ();
  503.     }
  504.   else if (*input_line_pointer == ',')
  505.     {
  506.       input_line_pointer++;
  507.       exp = get_absolute_expression ();
  508.     }
  509.   else
  510.     {
  511.       input_line_pointer = save;
  512.       exp = 0;
  513.     }
  514.   if (exp >= 1000)
  515.     {
  516.       as_bad ("subsegment index too high");
  517.     }
  518.  
  519.   if (strcmp (section_name, ".text") == 0)
  520.     {
  521.       subseg_set (SEG_TEXT, (subsegT) exp);
  522.     }
  523.  
  524.   if (strcmp (section_name, ".data") == 0)
  525.     {
  526.       if (flag_readonly_data_in_text)
  527.     subseg_set (SEG_TEXT, (subsegT) exp + 1000);
  528.       else
  529.     subseg_set (SEG_DATA, (subsegT) exp);
  530.     }
  531.  
  532.   *section_name_end = c;
  533. }
  534.  
  535. #endif /* ! BFD_ASSEMBLER */
  536.  
  537. /* end of obj-aout.c */
  538.